package io.github.wanggit.antrpc.client.zk.lb;

import io.github.wanggit.antrpc.client.zk.zknode.NodeHostEntity;
import io.github.wanggit.antrpc.commons.utils.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;

@Slf4j
public class RandomLoadBalancer extends AbstractLoadBalancer {

    @Override
    public NodeHostEntity choose() {
        if (null == entities || entities.isEmpty()) {
            throw new IllegalArgumentException("hostEntities cannot be null or empty.");
        }
        int size = entities.size();
        int idx = RandomUtils.nextInt(0, size);
        NodeHostEntity entity = entities.get(idx);
        if (log.isDebugEnabled()) {
            log.debug("Choosed " + JsonUtils.toJsonString(entity));
        }
        super.stats(entity);
        return entity;
    }
}
